<?php

namespace App\Repository\Shop;

use App\Entity\Shop\ShopProduct;
use App\Entity\Shop\ShopProductStorage;
use App\Repository\MyData\MyDataValueRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method ShopProductStorage|null find($id, $lockMode = null, $lockVersion = null)
 * @method ShopProductStorage|null findOneBy(array $criteria, array $orderBy = null)
 * @method ShopProductStorage[]    findAll()
 * @method ShopProductStorage[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ShopProductStorageRepository extends ServiceEntityRepository
{
    public $myDataValueRepository;
    public function __construct(ManagerRegistry $registry,MyDataValueRepository $myDataValueRepository)
    {
        parent::__construct($registry, ShopProductStorage::class);
        $this->myDataValueRepository = $myDataValueRepository;
    }

    public function updateProductStorageRows(ShopProduct $product)
    {
        $values = $this->myDataValueRepository->getValues4Entity($product->getParamTemplate(),ShopProduct::class,$product->getId());
//        dump($values);
//        $reformatArray = [];
        $mixedArray = [];
        foreach ($values as $row) {
            if ($row->getMyKey()->getType() == "arrayText") {
                $key = $row->getMyKey()->getName();
                $label = $row->getMyKey()->getLabel();
                $val = json_decode($row->getValue(),true);
//                $reformatArray[$key] = $values;
                if (!$mixedArray) {
                    foreach ($val as $index=>$value) {
                        $chain = $key.$index;
                        $text = $value;
                        $param = [
                            "name"=>$key,//英文字段，gg
                            "label"=>$label,//中文字段，规格
                            "value"=>$value,//中文字段，规格
                        ];
                        $mixedArray[] = [
                            'chain'=>$chain,
                            'text'=>$text,
                            'param'=>[$param],
                        ];
                    }
                }else{
                    $newMixedArray = [];
                    foreach ($val as $index=>$value) {
                        $chain = $key.$index;
                        $text = $value;
                        $param = [
                            "name"=>$key,//英文字段，gg
                            "label"=>$label,//中文字段，规格
                            "value"=>$value,//中文字段，规格
                        ];
                        foreach ($mixedArray as $one) {
                            array_push($one["param"],$param);
                            $newMixedArray[]= [
                                'chain'=>$one["chain"].",".$chain,
                                'text'=>$one["text"].",".$text,
                                'param'=>$one["param"],
                            ];
                        }
                    }
                    $mixedArray = $newMixedArray;
                }
            }
        }
        foreach ($mixedArray as $row) {
            $chain = $row["chain"];
            $text = $row["text"];
            $param = $row["param"];
            $log = $this->getRowByChain($product,$chain);

            if (!$log) {
                $log = new ShopProductStorage();
                $log->setProduct($product)
                    ->setIsOnSale(false)
                    ->setNum(0)
                    ->setPrice(0)
                    ->setPriceSale(0)
                    ->setPriceCost(0)
                    ->setText($text)
                    ->setChain($chain)
                    ->setParam($param);
                $this->_em->persist($log);
                $this->_em->flush();
            }

        }

        return $mixedArray;
    }

    public function getRowByChain(ShopProduct $product,$chain)
    {
        return $this->findOneBy(array(
            "product"=>$product,
            "chain"=>$chain
        ));
    }

//    public function mixArray($array ,&$mixedArray)
//    {
//        $mixedArray = [];
//        foreach ($a) {
//        }
//        if (count($array) <= 1) {
//
//        }
//    }



    // /**
    //  * @return ShopProductStorage[] Returns an array of ShopProductStorage objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('s')
            ->andWhere('s.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('s.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?ShopProductStorage
    {
        return $this->createQueryBuilder('s')
            ->andWhere('s.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
